package com.enjoy.studyfragment.fragment;

import android.accounts.AccountManager;
import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.enjoy.mjbaseframe.base.BaseMainFragment;
import com.enjoy.mjbaseframe.constant.BaseConstant;
import com.enjoy.mjbaseframe.constant.BaseVariable;
import com.enjoy.mjbaseframe.okgo.JsonCallback;
import com.enjoy.mjbaseframe.okgo.OkGoLoader;
import com.enjoy.mjbaseframe.util.GsonUtil;
import com.enjoy.mjbaseframe.util.PatternUtil;
import com.enjoy.mjbaseframe.util.SPUtil;
import com.enjoy.studyfragment.activity.MainActivity;
import com.enjoy.studyfragment.bean.UserTable;
import com.enjoy.studyfragment.constant.Constants;
import com.enjoy.studyfragment.R;
import com.enjoy.studyfragment.constant.Variable;
import com.orhanobut.logger.Logger;

import java.util.Map;
import java.util.WeakHashMap;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

/**
 * Created by Administrator on 2018/3/15.
 */

public class LoginFragment extends BaseMainFragment {

    @BindView(R.id.edtPhone)
    TextInputEditText edtPhone;
    @BindView(R.id.layouPhone)
    TextInputLayout layouPhone;
    @BindView(R.id.edtPwd)
    TextInputEditText edtPwd;
    @BindView(R.id.layoutPwd)
    TextInputLayout layoutPwd;
    @BindView(R.id.btnLogin)
    AppCompatButton btnLogin;

    @Override
    public Object setLayout() {
        return R.layout.fragment_login;
    }

    @Override
    public void init() {
        super.init();
    }

    @OnClick(R.id.btnLogin)
    public void onViewClicked() {
        String phone = edtPhone.getText().toString().trim();
        String pwd = edtPwd.getText().toString().trim();
        if(TextUtils.isEmpty(phone)){
            showError(layouPhone,"手机号不能为空");
            return;
        }
        if(!PatternUtil.isMobile(phone)){
            showError(layouPhone,"请输入正确的手机好");
            return;
        }
        if(TextUtils.isEmpty(pwd)){
            showError(layoutPwd,"密码不能为空不能为空");
            return;
        }
        if(!PatternUtil.isPWD(pwd)){
            showError(layoutPwd,"请输入正确的密码");
            return;
        }
        post(phone,pwd);
    }

    /**
     * 显示错误提示，并获取焦点
     * @param textInputLayout
     * @param error
     */
    private void showError(TextInputLayout textInputLayout, String error) {
        textInputLayout.setError(error);
        textInputLayout.getEditText().setFocusable(true);
        textInputLayout.getEditText().setFocusableInTouchMode(true);
        textInputLayout.getEditText().requestFocus();
    }

    //登录请求
    private void post(final String name, String pwd){
        Map map = new WeakHashMap();
        map.put("userNo", name);
        map.put("pwd", pwd);
        map.put("cid", Variable.CID);

        OkGoLoader.getInstance().post(Constants.APPLOGIN, map, new JsonCallback() {
            @Override
            public void onComplete(String result, String totalPageNum) {
                UserTable userTable = GsonUtil.getInstance().getObj(UserTable.class, result);
                userTable.setUser_no(name);
                if (userTable.getToken() != null) {
//                    SPUtil.put("token",userTable.getToken());
                    Logger.d( "token" + userTable.getToken()+"result:"+result);
                    BaseVariable.TOKEN = userTable.getToken();
                }

                SPUtil.setObjectToSP( userTable, Constants.USERTABLE);
                if (userTable.getList().size()==1){
                    SPUtil.setObjectToSP( userTable.getList().get(0),  Constants.CLASSLIST);

                }
                Variable.ISLOGIN = true;
                replaceFragment(new MainFragment(),false);

            }

            @Override
            public void onFailure(int status, String msg) {
                super.onFailure(status, msg);

            }
        });
    }
}
